from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import ListAPIView
from rest_framework.response import Response
from apps.meiduo_admin.serializers.sku import SKUSerializer,GoodsCategorySerializer,SPUSerializer,SPUSpecifationSerializer
from apps.goods.models import SKU,GoodsCategory,SPU,SPUSpecification

from apps.meiduo_admin.utils import PageNum


#GET /meiduo_admin/skus/?page=1&pagesize=10&keyword=
#sku商品列表和搜索列表的查询
class SKUModelViewSet(ModelViewSet):
    pagination_class = PageNum
    serializer_class = SKUSerializer
    

    #get_queryset 为GenericAPIView的方法，写此方法url需要加上basename=
    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return SKU.objects.filter(name__contains=keyword)
        else:
            return SKU.objects.all()



#获取三级分类信息GET/meiduo_admin/skus/categories/
class GoodsCategoryView(ListAPIView):
    queryset = GoodsCategory.objects.filter(subs=None)
    serializer_class = GoodsCategorySerializer

#获取SPU表名
class SPUView(ListAPIView):
    queryset = SPU.objects.all()
    serializer_class = SPUSerializer


#规格选项
from rest_framework.views import APIView
class SPUSpecView(APIView):

    def get(self,request,pk):
        specs = SPUSpecification.objects.filter(spu_id=pk)
        serializer = SPUSpecifationSerializer(specs,many=True)
        return Response(serializer.data)
